package logic

import (
	"context"
	"errors"

	"hm-zero-mall/apps/user/rpc/internal/svc"
	"hm-zero-mall/apps/user/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
	return &GetUserInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetUserInfo 获取用户信息
func (l *GetUserInfoLogic) GetUserInfo(in *pb.UserIdRequest) (*pb.UserInfo, error) {
	user, err := l.svcCtx.UserModel.FindOne(l.ctx, in.UserId)
	if err != nil {
		return nil, err
	}
	if user == nil || user.Id == 0 {
		return nil, errors.New("用户不存在")
	}

	return &pb.UserInfo{
		Id:         user.Id,
		Username:   user.Username,
		Password:   user.Password,
		Phone:      user.Phone,
		Status:     user.Status,
		CreateTime: user.CreateTime,
		UpdateTime: user.UpdateTime,
		DeleteTime: user.DeleteTime,
	}, nil
}
